<?php
/**
 * Created by PhpStorm.
 * User: 聂尧
 * Date: 2017/7/21
 * Time: 9:11
 * function:角色权限管理
 */

namespace app\admin\controller;
use       think\Controller;
use       think\Db;
use       think\Request;

class Role extends Controller
{

    //角色列表
    public  function index()
    {

        $count=Db::name('x_system_role')->count();
        $list =Db::name('x_system_role')->select();
        $this->assign('count',$count);
        $this->assign('list',$list);
        return $this->fetch();
    }

    //添加角色页面
    public function admin_role_add()
    {
        //先得到所有一级菜单
        $first_menu=Db::query("select m_id from x_system_menu where pid=:pid",['pid'=>0]);
        //转换成一维数组
        $power=array();
        for($i=0;$i<count($first_menu);$i++)
        {
           $power[]=$first_menu[$i]['m_id'];
        }


        //得到每个子菜单
        $menu=array();
        for($i=0;$i<count($power);$i++)
        {
            $data=Db::query("select * from x_system_menu where pid=:pid",['pid'=>$power[$i]]);
            if($data) {
                $menu[] = $data;
            }
            //清空数组
            unset($data);
        }
        $this->assign('count',count($menu));
        $this->assign('list',$menu);
        return $this->fetch();
    }


    //添加角色到数据库中

    public function role_add()
    {
        //得到传过来的数据
        $data   = Request::instance()->post('data', '', 'htmlspecialchars_decode');
        $data   = json_decode($data, true);
        $message= array();
        $string ="";
        //处理传过来的一级菜单和二级才菜单的ID
        for($i=2;$i<count($data)-1;$i++)
        {
               if($i==count($data)-1)
               {
                   $string.=$data[$i]['value'];
               }
               else {
                   $string .= $data[$i]['value'] . ",";
               }
        }
        $message['r_name']  =$data[0]['value'];
        $message['descripe']=$data[1]['value'];
        $message['power']   =$string;
        $offest=Db::name('x_system_role')->insert($message);
        if($offest) {
            return json(['code' => 1, 'msg' => '添加成功']);
        }
        return json(['code' => 0, 'msg' => '添加失败请重新添加']);
    }

    //编辑角色
    public function admin_role_edit()
    {
        $r_id=Request::instance()->get('r_id');
        $role=Db::name('x_system_role')->where('r_id',$r_id)->find();
        //先得到所有一级菜单
        $first_menu=Db::query("select m_id from x_system_menu where pid=:pid",['pid'=>0]);
        //转换成一维数组
        $power=array();
        for($i=0;$i<count($first_menu);$i++)
        {
            $power[]=$first_menu[$i]['m_id'];
        }


        //得到每个子菜单
        $menu=array();
        for($i=0;$i<count($power);$i++)
        {
            $data=Db::query("select * from x_system_menu where pid=:pid",['pid'=>$power[$i]]);
            if($data) {
                $menu[] = $data;
            }
            //清空数组
            unset($data);
        }
        $this->assign('count',count($menu));
        $this->assign('list',$menu);
        $this->assign('role',$role);

        return $this->fetch();
    }

    //更新单条数据
    public function role_update()
    {
        //得到传过来的数据
        $r_id   = Request::instance()->get('r_id');
        $data   = Request::instance()->post('data', '', 'htmlspecialchars_decode');
        $data   = json_decode($data, true);
        $message= array();
        $string ="";
        //处理传过来的一级菜单和二级才菜单的ID
        for($i=2;$i<count($data)-1;$i++)
        {
            if($i==count($data)-1)
            {
                $string.=$data[$i]['value'];
            }
            else {
                $string .= $data[$i]['value'] . ",";
            }
        }
        $message['r_name']  =$data[0]['value'];
        $message['descripe']=$data[1]['value'];
        $message['power']   =$string;
        $offest=Db::name('x_system_role')->where('r_id',$r_id)->update($message);
        if($offest) {
            return json(['code' => 1, 'msg' => '更新成功']);
        }
        return json(['code' => 0, 'msg' => '更新失败请重新更新']);
    }

    //删除角色
    public function role_delete()
    {

    }
}